﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class ChinhSachGiaHangHoa : MarshalByRefObject
    {
        static void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }

        public static Entities.ChinhSachGiaHangHoa[] Select()
        {
            string sql = Common.Constants.ChinhSachGiaHangHoa.Select;
            SqlConnection cn = DataAccess.Connection.OpenConnection();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            ArrayList arr = new ArrayList();
            while (dr.Read())
            {
                Entities.ChinhSachGiaHangHoa row = new Entities.ChinhSachGiaHangHoa();
                row.ChinhSachGiaHHID = int.Parse(dr[Common.Constants.ChinhSachGiaHangHoa.ChinhSachGiaHHID].ToString());
                row.MaChinhSachGiaHangHoa = dr[Common.Constants.ChinhSachGiaHangHoa.MaChinhSachGiaHangHoa].ToString();
                row.NgayLap = DateTime.Parse(dr[Common.Constants.ChinhSachGiaHangHoa.NgayLap].ToString());
                row.NgayBatDau = DateTime.Parse(dr[Common.Constants.ChinhSachGiaHangHoa.NgayBatDau].ToString());
                row.NgayKetThuc = DateTime.Parse(dr[Common.Constants.ChinhSachGiaHangHoa.NgayKetThuc].ToString());
                row.GhiChu = dr[Common.Constants.ChinhSachGiaHangHoa.GhiChu].ToString();
                row.Deleted = bool.Parse(dr[Common.Constants.ChinhSachGiaHangHoa.Deleted].ToString());
                arr.Add(row);
            }
            int n = arr.Count;
            if (n == 0) return null;

            Entities.ChinhSachGiaHangHoa[] arrC = new Entities.ChinhSachGiaHangHoa[n];
            for (int i = 0; i < n; i++)
            {
                arrC[i] = (Entities.ChinhSachGiaHangHoa)arr[i];
            }

            //Giai phong bo nho
            arr = null;
            cmd.Connection.Dispose();
            cn.Close();
            cn = null;
            return arrC;

        }

        public static bool InsertUpdate(Entities.ChinhSachGiaHangHoa chinhsachgiahanghoa)
        {
            try
            {
                string sql = Common.Constants.ChinhSachGiaHangHoa.InsertUpdate;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.HanhDong, SqlDbType.NVarChar, 20).Value = chinhsachgiahanghoa.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.ChinhSachGiaHHID, SqlDbType.Int).Value = chinhsachgiahanghoa.ChinhSachGiaHHID;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.MaChinhSachGiaHangHoa, SqlDbType.VarChar, 50).Value = chinhsachgiahanghoa.MaChinhSachGiaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.NgayLap, SqlDbType.DateTime).Value = chinhsachgiahanghoa.NgayLap;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.NgayBatDau, SqlDbType.DateTime).Value = chinhsachgiahanghoa.NgayBatDau;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.NgayKetThuc, SqlDbType.DateTime).Value = chinhsachgiahanghoa.NgayKetThuc;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.GhiChu, SqlDbType.NVarChar, 200).Value = chinhsachgiahanghoa.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.Deleted, SqlDbType.Bit).Value = chinhsachgiahanghoa.Deleted;
                int i = cmd.ExecuteNonQuery();
                bool kt = false;
                if (i == 1)
                {
                    kt = true;

                    if (chinhsachgiahanghoa.HanhDong == Common.Constants.Insert)
                    {
                        LuuViet
                            (
                            "Thêm",
                            chinhsachgiahanghoa.TaiKhoan.MaNhanVien,
                            "Thêm chính sách giá hàng hóa " + chinhsachgiahanghoa.MaChinhSachGiaHangHoa,
                            chinhsachgiahanghoa.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }
                    else
                    {
                        LuuViet
                            (
                            "Sửa",
                            chinhsachgiahanghoa.TaiKhoan.MaNhanVien,
                            "Sửa chính sách giá hàng hóa " + chinhsachgiahanghoa.MaChinhSachGiaHangHoa,
                            chinhsachgiahanghoa.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }
                }
                else
                    kt = false;

                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool Delete(Entities.ChinhSachGiaHangHoa chinhsachgiahanghoa)
        {
            try
            {
                bool kt = false;
                string sql = Common.Constants.ChinhSachGiaHangHoa.Delete;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.HanhDong, SqlDbType.NVarChar, 20).Value = chinhsachgiahanghoa.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChinhSachGiaHangHoa.ChinhSachGiaHHID, SqlDbType.Int).Value = chinhsachgiahanghoa.ChinhSachGiaHHID;
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    kt = true;
                    if (chinhsachgiahanghoa.HanhDong == Common.Constants.Insert)
                    {
                        LuuViet
                            (
                            "Xóa",
                            chinhsachgiahanghoa.TaiKhoan.MaNhanVien,
                            "Xóa chính sách giá hàng hóa " + chinhsachgiahanghoa.MaChinhSachGiaHangHoa,
                            chinhsachgiahanghoa.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }
                }
                else
                    kt = false;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }


        /// <summary>
        ///  Select hàng hóa và gói hàng
        /// </summary>
        /// <returns></returns>
        public static string DonViTinh(string madonvitinh)
        {
            string tralai = "";
            try
            {
                Entities.DVT[] donvitinh = BusinessLogic.DVT.Select();
                for (int k = 0; k < donvitinh.Length; k++)
                {
                    if (donvitinh[k].MaDonViTinh == madonvitinh)
                    {
                        tralai = donvitinh[k].TenDonViTinh;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; tralai = ""; }
            return tralai;
        }

        // Select Hang hoa co phan tram khuyen mai
        public static Entities.HangHoa[] SelectHangHoa()
        {
            Entities.HangHoa[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HangHoa.selectTatCaHangHoa, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.TenDonViTinh = DonViTinh(dr[Common.Constants.HangHoa.MaDonViTinh].ToString());
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    row.Phantrankhuyenmai = Khuyenmai(dr[Common.Constants.HangHoa.MaHangHoa].ToString());
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());

                    arr.Add(row);
                }
                if (arr.Count > 0)
                {
                    list = (Entities.HangHoa[])arr.ToArray(typeof(Entities.HangHoa));
                }
                else
                {
                    list = new Entities.HangHoa[0];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = new Entities.HangHoa[0]; }
            return list;
        }
        public static string Khuyenmai(string mahanghoa)
        {
            string str = "";
            try
            {
                Entities.ChiTietChinhSachGiaHangHoa[] ChinhSachGia = BusinessLogic.ChiTietChinhSachGiaHangHoa.Select();
                for (int i = 0; i < ChinhSachGia.Length; i++)
                {
                    if (ChinhSachGia[i].MaHangHoa == mahanghoa)
                    {
                        str = ChinhSachGia[i].PhanTramGiaBanBuon;
                        break;
                    }
                }
            }
            catch
            {
                str = "";
            }
            return str;
        }

        public static Entities.ChinhSachGiaHangHoa SelectCSGHH(Entities.GoiHang gh)
        {
            Entities.ChinhSachGiaHangHoa kh = null;
            try
            {
                kh = new Entities.ChinhSachGiaHangHoa();
                kh.HangHoa = SelectHangHoa();
                kh.GoiHang = BusinessLogic.GoiHang.Select(gh);
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); kh = null; }

            return kh;
        }
    }
}
#endregion